<?php
/**
* Defautl page if action is unknown
*
* @author       Fabrice Douteaud <clearbudget@douteaud.com>
* @package      actions
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice douteaud (clearbudget@douteaud.com)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/
//prevent direct access
if(!defined('ENGINEON')) die('Direct access forbidden');

$report = class_report::getInstance();
$report->getData();
if($report->validData === false) {
  global $context;
  $context->error = 'error_NoTransactionData';
  $context->redirect('errorDisplay');
  return;
  }
$dates = $report->dates;
$categories = $report->categories;
$categoryColor = $report->categoryColor;
$debitTotalPerCategory = $report->debitTotalPerCategory;
$creditTotalPerCategory = $report->creditTotalPerCategory;
$debitsPctPerCategory = $report->debitPctPerCategory;
$creditsPctPerCategory = $report->creditPctPerCategory;
$totalDebitsPerMonth = $report->debitTotalPerMonth;
$totalCreditsPerMonth = $report->creditTotalPerMonth;
$totalDebits = $report->totalDebits;
$totalCredits = $report->totalCredits;
$debitsPctPerMonth = $report->debitPctPerMonth;
$creditsPctPerMonth = $report->creditPctPerMonth;
$debitPerCategoryPerMonth = $report->debitPerCategoryPerMonth;
$creditPerCategoryPerMonth = $report->creditPerCategoryPerMonth;
$colCount = count($dates) + 4; // 4 for the extra columns (category, total, %, avg)
$availableReportRanges = $report->getAvailableReportTimeRanges();

// tag cloud per category spending
// top 10 categories
// sort by value
if(count($categories) > 1) {
  $cloudTop10 = $debitsPctPerCategory;
  asort($cloudTop10, SORT_NUMERIC);
  $cloudTop10 = array_reverse($cloudTop10, true);
  // redo a nice array sorted by value and with the category name
  $tag = count($cloudTop10);
  if($tag > 10) $tag = 10;
  foreach($cloudTop10 as $id=>$pct) {
    $cloud[$report->getCategoryName($id)]['tag'] = $tag;
    $cloud[$report->getCategoryName($id)]['catId'] = $id;
    $tag--;
    if($tag<1) $tag = 1;
    }
  // order this array by alphabetical order
  ksort($cloud, SORT_STRING);
  }
else {
  $cloud = false;
  }

// top 10 categories overall spending
$chart = new GoogleChart('p3', '', 550, 200); // $keys->graphTitle_topsDebitsOverall
if(count($debitTotalPerCategory) > 0) {
  // sort by value
  asort($debitTotalPerCategory, SORT_NUMERIC);
  $debitTotalPerCategory = array_reverse($debitTotalPerCategory, true);
  // keep only the top 10 items
  $topsDebitsPerCategory = array_slice($debitTotalPerCategory, 0, 10, true);
  // keep all other items
  $otherDebits = array_slice($debitTotalPerCategory, 10, null, true);
  // build the colors array
  foreach($topsDebitsPerCategory as $key=>$val) {
    $pctVal = $report->formatNumber($val / $totalDebits * 100);
    if($key>1) {
      $chart->addLegend($report->getCategoryName($key).' ('.$pctVal.'%)');
      $chart->addValue($pctVal);
      $chart->addColor($categoryColor[$key]);
      }
    else {
      $otherDebits[$key] = $val;
      }
    }
  // compute the overall value of all 'others'
  $tmpVal = 0;
  foreach($otherDebits as $key=>$val) {
    $tmpVal += $val;
    }
  $tmpVal = $report->formatNumber($tmpVal / $totalDebits * 100);
  $chart->addLegend('Others ('.$tmpVal.'%)');
  $chart->addValue($tmpVal);
  $chart->addColor('444444');
  $url3 = $chart->getURL();
  }

// top 10 categories monthly spending
$chart = new GoogleChart('bvs', '', 450, 200); //$keys->graphTitle_topsDebitsMonthly
// reverse the array as to get the smallest first
$topsDebitsPerCategory = array_reverse($topsDebitsPerCategory, true);
// go through each item
foreach($topsDebitsPerCategory as $category=>$val) {
  // if category is uncategorized we skip it
  if($category === 1) continue;
  // graph legend (category name)
  $chart->addLegend($report->getCategoryName($category));
  // category color
  $chart->addColor($categoryColor[$category]);
  // initialize the other array
  $others['total'] = 0;
  // go through each month
  foreach($dates as $date) {
    
    // check if there is spending for that month
    if(isset($debitPerCategoryPerMonth[$category][$date])) { 
      $pctVal = $report->formatNumber($debitPerCategoryPerMonth[$category][$date] / $totalDebitsPerMonth[$date] * 100);
      $chart->addValue($pctVal);
      @$others['total'][$date] += $pctVal;
      }
    else {
      $chart->addValue('0.0');
      @$others['total'][$date] += 0; 
      }
    }
  $chart->addValueSerie();
  }
$chart->addColor('444444');
$chart->addLegend('Others');
foreach($dates as $date) {
  if(isset($others['total'][$date])) $chart->addValue(100-$others['total'][$date]);
  else $chart->addValue(100);
  $month = explode('-', $date);
  $chart->addxAxis($month[0]);
  }
$chart->addyAxis('0|20|40|60|80|100');
$url4 = $chart->getURL();
?>